/**
 *  Copyright (C) 2011  Thomas ROBIN
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package triseries;

import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author tshepo
 * 
 */
public class FileAcces {

	private static final boolean DEBUG = (Main.debug || MyProperties.debug.equalsIgnoreCase("FileAcces"));
	private static Desktop desktop;

	public static void saveForLater(String texte) {
		if(DEBUG) System.err.println("FileAcces.saveForLater("+texte+")");

		FileWriter writer = null;
		try {
			writer = new FileWriter(".Sub_unavailables", true);
			writer.write(texte, 0, texte.length());
		} catch (IOException ex) {
			Main.processExeption("FileAcces.saveForLater :\n"
					+ ex.getStackTrace().toString());
		} finally {
			if (writer != null) {
				try {
					writer.close();
				} catch (IOException ex) {
					Main.processExeption("FileAcces.saveForLater :\n"
							+ ex.getStackTrace().toString());
				}
			}
		}
	}

	public static List<String> getSavedFile() {

		if(DEBUG) System.err.println("FileAcces.getSavedFile()");

		List<String> retour = new ArrayList<String>();

		File fichier = new File(".unavailables");
		if (fichier.exists()) {
			InputStream ips = null;
			try {
				ips = new FileInputStream(".unavailables");
			} catch (FileNotFoundException e) {
				Main.processExeption("FileAcces.getSavedFile :\n"
						+ e.getStackTrace().toString());
			}
			InputStreamReader ipsr = new InputStreamReader(ips);
			BufferedReader br = new BufferedReader(ipsr);
			String ligne;
			try {
				while ((ligne = br.readLine()) != null) {
					if (!retour.contains(ligne)) {
						retour.add(ligne);
					}
				}
			} catch (IOException e) {
				Main.processExeption("FileAcces.getSavedFile :\n"
						+ e.getStackTrace().toString());
			}
			try {
				br.close();
			} catch (IOException e) {
				Main.processExeption("FileAcces.getSavedFile :\n"
						+ e.getStackTrace().toString());
			}
			fichier.delete();
		}
		return retour;
	}

	public static List<String> getSavedSub() {
		if(DEBUG) System.err.println("FileAcces.getSavedSub()");

		List<String> retour = new ArrayList<String>();
		File fichier = new File(".Sub_unavailables");
		if (fichier.exists()) {
			InputStream ips = null;
			try {
				ips = new FileInputStream(".Sub_unavailables");
			} catch (FileNotFoundException e) {
				Main.processExeption("FileAcces.getSavedSub :\n"
						+ e.getStackTrace().toString());
			}
			InputStreamReader ipsr = new InputStreamReader(ips);
			BufferedReader br = new BufferedReader(ipsr);
			String ligne;
			try {
				while ((ligne = br.readLine()) != null) {
					if (!retour.contains(ligne)) {
						retour.add(ligne);
					}
				}
			} catch (IOException e) {
				Main.processExeption("FileAcces.getSavedSub :\n"
						+ e.getStackTrace().toString());
			}
			try {
				br.close();
			} catch (IOException e) {
				Main.processExeption("FileAcces.getSavedSub :\n"
						+ e.getStackTrace().toString());
			}
			fichier.delete();
		}
		return retour;
	}

	public static void openProperties() {
		if(DEBUG) System.err.println("FileAcces.openProperties()");
		if (Desktop.isDesktopSupported()) {
			// On récupère l'instance du desktop :
			desktop = Desktop.getDesktop();
			// On vérifie que la fonction open est bien supportée :
			if (desktop.isSupported(Desktop.Action.OPEN)) {
				try {
					File f = new File("Settings.txt");
					if (f.exists()) {
						desktop.open(f);
					}
				} catch (IOException ex) {
					Main.processExeption("FileAcces.openProperties :\n"
							+ ex.getStackTrace().toString());
				}
			}
		}
	}

	public static boolean Exist(String s) {
		return new File(s).exists();
	}

	public static boolean openFile(String s) {
		if(DEBUG) System.err.println("FileAcces.openFile("+s+")");
		if (Desktop.isDesktopSupported()) {
			// On récupère l'instance du desktop :
			desktop = Desktop.getDesktop();
			// On vérifie que la fonction open est bien supportée :
			if (desktop.isSupported(Desktop.Action.OPEN)) {
				File f = new File(s);
				if (f.exists()) {
					try {
						desktop.open(f);
						return true;
					} catch (IOException ex) {
						Main.processExeption("FileAcces.openFile :\n"
								+ ex.getStackTrace().toString());
					}
				}
			}
		}
		return false;
	}

	static void ImportResource(String string, String resource) {
		if(DEBUG) System.err.println("FileAcces.ImportResource("+string+","+resource+")");
		
		File f = new File(string);
		InputStream inputStream = Main.class.getResourceAsStream(resource);
		OutputStream out = null;
		try {
			out = new FileOutputStream(f);
		} catch (FileNotFoundException e) {
			Main.processExeption("FileAcces.ImportResource :\n"+ e.getStackTrace().toString());
		}
		byte buf[] = new byte[1024];
		int len;
		try {
			while ((len = inputStream.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
		} catch (IOException e) {
			Main.processExeption("FileAcces.ImportResource :\n"+ e.getStackTrace().toString());
		}
		try {
			out.close();
		} catch (IOException e) {
			Main.processExeption("FileAcces.ImportResource :\n"+ e.getStackTrace().toString());
		}
		try {
			inputStream.close();
		} catch (IOException e) {
			Main.processExeption("FileAcces.ImportResource :\n"+ e.getStackTrace().toString());
		}
	}

	public static boolean openFile(String file, String[] exts) {
		if(DEBUG) System.err.println("FileAcces.openFile("+file+")");
		for (String ext : exts) {
			if (openFile(file + "." + ext))
				return true;
		}
		return false;
	}

	public static void bakupSub() {
		if(DEBUG) System.err.println("FileAcces.bakupSub()");
		if (Exist(".Sub_unavailables")) {
			File fichier = new File(".Sub_unavailables");
			File bakup = new File(".Sub_unavailables.bak");
			MoveFile.copieFile(fichier, bakup);
		}
	}

	public static void delBakupSub() {
		if(DEBUG) System.err.println("FileAcces.delBakupSub()");
		File fichier = new File(".Sub_unavailables.bak");
		fichier.delete();
	}
}
